//
//  WDGroMoreJosnManager.m
//  WDGroMore
//
//  Created by little People on 2023/7/31.
//

#import "WDGroMoreJosnManager.h"
#define WDGroMoreJsonFileName @"GroMoreConfig"
@implementation WDGroMoreJosnManager

#pragma mark - Public API

+ (BOOL)isSplashAdEnabledForColdLaunch {
    return [[self splashAdConfig][@"sdk_ad_splash_cool"] boolValue];
}

+ (BOOL)isSplashAdEnabledForHotLaunch {
    return [[self splashAdConfig][@"sdk_ad_splash_hot"] boolValue];
}

+ (NSDictionary *)readWDGroMoreSDKConfigFile {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:WDGroMoreJsonFileName ofType:@"json"];
    if (!filePath) {
        NSLog(@"[GroMoreConfig] JSON file not found.");
        return @{};
    }

    NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
    if (!jsonData) {
        NSLog(@"[GroMoreConfig] Failed to load data from file.");
        return @{};
    }

    NSError *error = nil;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    
    if (error || ![jsonDict isKindOfClass:[NSDictionary class]]) {
        NSLog(@"[GroMoreConfig] JSON parsing error: %@", error.localizedDescription);
        return @{};
    }

    return jsonDict;
}

#pragma mark - Internal

+ (NSDictionary *)splashAdConfig {
    NSDictionary *config = [self readWDGroMoreSDKConfigFile];
    return [config isKindOfClass:[NSDictionary class]] ? config : @{};
}


+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return @{};
    }

    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
//        WDLog(@"json解析失败：%@",err);
        return @{};
    }
    return dic;
}

+ (NSString *)bmsAppNameForInfoPlistFile {
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"bms"];
    NSArray *keys = [configDict allKeys];
    if (![keys containsObject:@"app_name"])
    {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",configDict[@"app_name"]];
}

+ (NSString *)bmsAppVersionForInfoPlistFile {
    NSString *bmsAppVersion = [[NSBundle mainBundle].infoDictionary objectForKey:@"BMSVersion"];
    if (bmsAppVersion.length > 0)
    {
        return bmsAppVersion;
    }
    return @"";
}

+ (NSString *)bmsChannelForSDKConfigFile {
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"bms"];
    NSArray *keys = [configDict allKeys];
    if (![keys containsObject:@"channel"])
    {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",configDict[@"channel"]];
}

+ (NSString *)thinkingAppidForSDKConfigFile {
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"Thinking"];
    NSArray *keys = [configDict allKeys];
    if (![keys containsObject:@"appid"])
    {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",configDict[@"appid"]];
}

+ (NSString *)thinkingServerUrlForSDKConfigFile {
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"Thinking"];
    NSArray *keys = [configDict allKeys];
    if (![keys containsObject:@"server_url"])
    {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",configDict[@"server_url"]];
}
+ (NSString *)UMAppkeyForSDKConfigFile {
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSArray *keys = [dict allKeys];
    if (![keys containsObject:@"umAppkey"])
    {
        return @"";
    }
    return  [NSString stringWithFormat:@"%@",dict[@"umAppkey"]];;
}

+ (NSString *)GroMoreAppidForSDKConfigFile {
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"GroMore"];
    NSArray *keys = [configDict allKeys];
    if (![keys containsObject:@"groMoreAppId"])
    {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",configDict[@"groMoreAppId"]];
}

+ (NSString *)GroMoreRewardAdIdForSDKConfigFile {
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"GroMore"];
    NSArray *keys = [configDict allKeys];
    if (![keys containsObject:@"rewardAdId"])
    {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",configDict[@"rewardAdId"]];
}

+ (NSString *)GroMoreInterstitialIdForSDKConfigFile
{
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"GroMore"];
    NSArray *keys = [configDict allKeys];
    if (![keys containsObject:@"interstitialId"])
    {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",configDict[@"interstitialId"]];
}

+ (NSString *)GroMoreNativeIdForSDKConfigFile
{
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"GroMore"];
    NSArray *keys = [configDict allKeys];
    if (![keys containsObject:@"nativeAdId"])
    {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",configDict[@"nativeAdId"]];
}

+ (NSString *)GroMoreSplashIdForSDKConfigFile {
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"GroMore"];
    NSArray *keys = [configDict allKeys];
    if (![keys containsObject:@"splashAdId"])
    {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",configDict[@"splashAdId"]];
}

+ (NSString *)GroMoreBannerAdIdForSDKConfigFile
{
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"GroMore"];
    NSArray *keys = [configDict allKeys];
    if (![keys containsObject:@"bannerAdId"])
    {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",configDict[@"bannerAdId"]];
}


+ (NSString *)inAppPruchaseAdjustEventKeyForSDKConfigFile
{
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"gpConfig"];
    NSString *eventName = [[configDict allKeys] containsObject:@"adjust_event_key"]?[NSString stringWithFormat:@"%@",configDict[@"adjust_event_key"]]:@"";
    return eventName;
}

+ (NSString *)isAppKeyForSDKConfigFile
{
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    return [NSString stringWithFormat:@"%@",dict[@"isAppKey"]];
}

+ (NSArray *)consumableProductIdForSDKConfigFile
{
    NSMutableArray *tempArray = [NSMutableArray array];
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"gpConfig"];
    NSArray *consumableIds = configDict[@"consumableIds"];
    if (consumableIds.count <= 0)
    {
        return @[];
    }
    for (int i = 0; i < consumableIds.count; i++) {
        NSDictionary *dict = consumableIds[i];
        NSString *productId = dict[@"productId"];
        [tempArray addObject:productId];
    }
    return tempArray;
}

+ (NSArray *)nonConsumableProductIdForSDKConfigFile
{
    NSMutableArray *tempArray = [NSMutableArray array];
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"gpConfig"];
    NSArray *nonConsumableIds = configDict[@"nonConsumableIds"];
    if (nonConsumableIds.count <= 0)
    {
        return @[];
    }
    for (int i = 0; i < nonConsumableIds.count; i++) {
        NSDictionary *dict = nonConsumableIds[i];
        NSString *productId = dict[@"productId"];
        [tempArray addObject:productId];
    }
    
    return [NSArray arrayWithArray:tempArray];
}

+ (NSArray *)subscribeProductIdForSDKConfigFile
{
    NSMutableArray *tempArray = [NSMutableArray array];
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"gpConfig"];
    NSArray *subscribeIds = configDict[@"subscribeIds"];
    if (subscribeIds.count <= 0)
    {
        return @[];
    }
    for (int i = 0; i < subscribeIds.count; i++) {
        NSDictionary *dict = subscribeIds[i];
        NSString *productId = dict[@"productId"];
        [tempArray addObject:productId];
    }
    
    return [NSArray arrayWithArray:tempArray];
}

+ (NSArray *)productIdListForSDKConfigFile
{
    NSMutableArray *tempArray = [NSMutableArray array];
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"gpConfig"];
    NSArray *consumableIds = configDict[@"consumableIds"];
    if (consumableIds.count > 0)
    {
        for (int i = 0; i < consumableIds.count; i++) {
            NSDictionary *dict = consumableIds[i];
            NSString *productId = dict[@"productId"];
            [tempArray addObject:productId];
        }
    }
    
    NSArray *nonConsumableIds = configDict[@"nonConsumableIds"];
    if (nonConsumableIds.count > 0)
    {
        for (int i = 0; i < nonConsumableIds.count; i++) {
            NSDictionary *dict = nonConsumableIds[i];
            NSString *productId = dict[@"productId"];
            [tempArray addObject:productId];
        }
    }
    
    
    NSArray *subscribeIds = configDict[@"subscribeIds"];
    if (subscribeIds.count > 0)
    {
        for (int i = 0; i < subscribeIds.count; i++) {
            NSDictionary *dict = subscribeIds[i];
            NSString *productId = dict[@"productId"];
            [tempArray addObject:productId];
        }
    }
    
    
    return [NSArray arrayWithArray:tempArray];
}

+ (NSString *)renvenueWithProductId:(NSString *)productId
{
    NSMutableArray *tempArray = [NSMutableArray array];
    NSDictionary *dict = [self readWDGroMoreSDKConfigFile];
    NSDictionary *configDict = [dict objectForKey:@"gpConfig"];
    NSArray *consumableIds = configDict[@"consumableIds"];
    NSArray *nonConsumableIds = configDict[@"nonConsumableIds"];
    NSArray *subscribeIds = configDict[@"subscribeIds"];
    if (consumableIds.count > 0)
    {
        [tempArray addObjectsFromArray:consumableIds];
    }
    if (nonConsumableIds.count > 0)
    {
        [tempArray addObjectsFromArray:nonConsumableIds];
    }
    if (subscribeIds.count > 0)
    {
        [tempArray addObjectsFromArray:subscribeIds];
    }
    
    if (tempArray.count <= 0)
    {
        return @"";
    }
    
    for (int i = 0; i < tempArray.count; i++) {
        NSDictionary *dict = tempArray[i];
        NSString *pid = dict[@"productId"];
        NSString *dollar_price = [NSString stringWithFormat:@"%@",dict[@"dollar_price"]];
        if ([pid isEqualToString:productId])
        {
            // 假设你有一个可能表示数字的NSString
            NSString *numberString = dollar_price; // 这个例子中不是一个有效的数字字符串

            // 创建一个NSNumberFormatter实例
            NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];

            // 设置编号格式，这里使用无分组分隔符的十进制
            [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

            // 使用numberFromString:方法尝试将NSString转换为NSNumber
            NSNumber *number = [numberFormatter numberFromString:numberString];

            // 检查转换是否成功
            if (number != nil) {
                // 如果是数字，执行乘以100并取整数部分的操作
                double numberValue = [number doubleValue];
                double multipliedNumber = numberValue * 100;
                double integerPart = floor(multipliedNumber);
                // 返回格式化的字符串
                NSString *resultString = [NSString stringWithFormat:@"%.0f", integerPart];
                NSLog(@"%@", resultString);
                
                return resultString;
            } else {
                // 如果不是数字，返回"0"
                NSLog(@"不是数字，返回0");
                NSString *resultString = @"0";
                NSLog(@"%@", resultString);
                return resultString;

            }
            
            return dollar_price;
        }
    }
    
    return @"";
}

@end
